package com.tsugun.mediaplayer.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.model.entity.MusicModel;
import com.tsugun.mediaplayer.presenter.utils.ConverterHelper;
import com.tsugun.mediaplayer.presenter.utils.SettingPreferenceHelper;
import com.tsugun.mediaplayer.view.activity.BaseActivity;

/**
 * 播放列表会话框
 * Created by shize on 2017/5/22.
 */

public class MusicMenuDialog extends Dialog {
    private BaseActivity baseActivity;

    public MusicMenuDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        baseActivity = (BaseActivity) context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindow();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        if (window != null) {
            SettingPreferenceHelper.INSTANCE.setNightModeTheme(window.getDecorView(), baseActivity);
        }
    }

    /**
     * 初始化会话框
     */
    private void initWindow() {
        Window w = getWindow();
        if (w == null) {
            return;
        }
        w.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        lp.windowAnimations = R.style.PlayListAnimation;
        w.setAttributes(lp);
    }

    public static class Builder {

        private Context mContext;
        private RecyclerView.Adapter<? extends RecyclerView.ViewHolder> mAdapter;
        private MusicMenuDialog mDialog;
        private String mTitle; // 头部标题

        public Builder(Context context, RecyclerView.Adapter<? extends RecyclerView.ViewHolder> adapter,
                       String title) {
            mContext = context;
            mTitle = title;
            mAdapter = adapter;
        }

        /**
         * 创建会话框
         *
         * @return 会话框
         */
        public MusicMenuDialog create() {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            mDialog = new MusicMenuDialog(mContext, R.style.PlayListDialog);
            View layout = inflater.inflate(R.layout.layout_music_menu, null);
            mDialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            initList(layout);
            mDialog.setContentView(layout);
            return mDialog;
        }

        /**
         * 初始化列表
         *
         * @param layout 布局视图
         */
        private void initList(View layout) {
            RecyclerView list = (RecyclerView) layout.findViewById(R.id.id_music_menu_recycler);
            if (mAdapter == null) {
                return;
            }
            if (mAdapter.getItemCount() > 6) {
                ViewGroup.LayoutParams lp = list.getLayoutParams();
                lp.height = (int) ConverterHelper.INSTANCE.dipToPx(mContext, 360);
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
                list.setLayoutParams(lp);
            }
            list.setAdapter(mAdapter);
            list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
            TextView title = (TextView) layout.findViewById(R.id.id_txt_music);
            title.setText(mTitle);
            list.setBackgroundColor(SettingPreferenceHelper.INSTANCE.getBACKGROUND_COLOR());
        }
    }

    /**
     * 菜单显示音乐信息
     */
    public static class BuilderInfoDialog {

        private Context mContext;
        private MusicModel mMusicModel; // 音乐
        private MusicMenuDialog mDialog;

        public BuilderInfoDialog(Context context, MusicModel musicModel) {
            mContext = context;
            mMusicModel = musicModel;
        }

        /**
         * @return 创建并返回会话框
         */
        public MusicMenuDialog create() {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            mDialog = new MusicMenuDialog(mContext, R.style.PlayListDialog);
            View layout = inflater.inflate(R.layout.layout_music_info, null);
            mDialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            initLayout(layout);
            mDialog.setContentView(layout);
            return mDialog;
        }

        /**
         * 初始化布局
         *
         * @param layout layout root
         */
        private void initLayout(View layout) {
            TextView tvTitle = (TextView) layout.findViewById(R.id.id_music_info_title);
            tvTitle.setText(mMusicModel.getTitle());
            TextView tvName = (TextView) layout.findViewById(R.id.id_music_info_name);
            tvName.setText(ConverterHelper.INSTANCE.getFolderName(mMusicModel.getUrl()));
            TextView tvDuration = (TextView) layout.findViewById(R.id.id_music_info_duration);
            tvDuration.setText(ConverterHelper.INSTANCE.getConvertedTime(mMusicModel.getDuration(),
                    ConverterHelper.INSTANCE.getACCURATE_TO_MINUTE()));
            TextView tvBit = (TextView) layout.findViewById(R.id.id_music_info_bit);
            tvBit.setText(ConverterHelper.INSTANCE.getConvertedBit(mMusicModel.getBit()));
            TextView tvSize = (TextView) layout.findViewById(R.id.id_music_info_size);
            tvSize.setText(ConverterHelper.INSTANCE.getConvertedSize(mMusicModel.getSize()));
            TextView tvPath = (TextView) layout.findViewById(R.id.id_music_info_path);
            tvPath.setText(mMusicModel.getUrl());
        }

    }
}
